package com.lalosoft.gastostarjetaweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;
import com.lalosoft.gastostarjetaweb.dao.GmailAccountDao;
import com.lalosoft.gastostarjetaweb.model.GmailAccount;

@SuppressWarnings("serial")
public class RecuperarKeyServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String token = req.getParameter("token");
			
			GmailAccount account = GmailAccountDao.getAccount(token);
			JSONObject jsonObject = new JSONObject();
		
			jsonObject.put("password", account.getPassword());
			jsonObject.put("key", account.getAesKey());
			
			resp.setContentType("application/json");
			// Get the printwriter object from response to write the required json object to the output stream      
			PrintWriter out = resp.getWriter();
			// Assuming your json object is **jsonObject**, perform the following, it will return your json object  
			out.print(jsonObject);
			out.flush();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
}
